//
//  detaiPageForPoductionTableViewCell.m
//  IT爱好者
//
//  Created by dllo on 15/10/8.
//  Copyright © 2015年 Fan. All rights reserved.
//

#import "detaiPageForPoductionTableViewCell.h"
#import "Header.h"
#import "UIImageView+WebCache.h"
@implementation detaiPageForPoductionTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatView];
    }
    return self;
}

-(void)creatView
{
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.font = contentTextSize;
    
    _nameLabel.numberOfLines = 0;
    [self.contentView addSubview:_nameLabel];
    
    _thisWeekHitLabel = [[UILabel alloc]init];
    _thisWeekHitLabel.font = otherTextSize;
    
    [self.contentView addSubview:_thisWeekHitLabel];
    
    _pricelabel = [[UILabel alloc]init];
    //_pricelabel.backgroundColor = [UIColor blueColor];
    _pricelabel.textColor = [UIColor colorWithRed:0.95 green:0.61 blue:0.61 alpha:1];
    _pricelabel.font = contentTextSize;
    _pricelabel.layer.cornerRadius = 5.0f;//*****************为啥不好用??????
    _pricelabel.clipsToBounds = YES;
    [self.contentView addSubview:_pricelabel];
    
    _star1ImgeView = [[UIImageView alloc]init];
    [self.contentView addSubview:_star1ImgeView];
    
    _star2ImgeView = [[UIImageView alloc]init];
    [self.contentView addSubview:_star2ImgeView];

    
    _star3ImgeView = [[UIImageView alloc]init];
    [self.contentView addSubview:_star3ImgeView];

    
    _star4ImgeView = [[UIImageView alloc]init];
    [self.contentView addSubview:_star4ImgeView];
    
    _star5ImgeView = [[UIImageView alloc]init];
    [self.contentView addSubview:_star5ImgeView];
    
    _reviewNumLabel = [[UILabel alloc]init];
    _reviewNumLabel.font = otherTextSize;
    
    [self.contentView addSubview:_reviewNumLabel];
    
    _imgsrcView = [[UIImageView alloc]init];
    [self.contentView addSubview:_imgsrcView];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _nameLabel.textColor = contenttextColor;
    _thisWeekHitLabel.textColor = contenttextColor;
    _reviewNumLabel.textColor = otherTextColor;
#pragma mark 关键字高亮
    if (_dataModel.name && _keyWord) {
        //关键字标红效果
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:_dataModel.name];
        //获取关键字的位置
        NSRange range = [_dataModel.name rangeOfString:_keyWord];
        
        //设置关键字属性
        [attributeString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:15], NSFontAttributeName, nil] range:range];
        
        //添加到label
        _nameLabel.attributedText = attributeString;
    }
    
    //标题自适应高度
    _imgsrcView.frame = CGRectMake(10 * FitWidth, 20 * FitHeight, 80 * FitWidth, 60 * FitHeight);
    //NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:contentTextSize, NSFontAttributeName, nil];
    //CGRect rect = [_nameLabel.text boundingRectWithSize:CGSizeMake(250 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //标签自适应长度
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:contentTextSize, NSFontAttributeName, nil];
    CGRect rect2 = [_pricelabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 20 * FitHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic2 context:nil];
    CGRect rect3 = [_reviewNumLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 20 * FitHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic2 context:nil];
    
    _nameLabel.frame = CGRectMake(_imgsrcView.frame.origin.x + _imgsrcView.frame.size.width + 10 * FitWidth, _imgsrcView.frame.origin.y - 10 * FitWidth, 250 * FitWidth, 20 * FitHeight);
    _thisWeekHitLabel.frame = CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 10 * FitHeight, 100 * FitWidth, 20 * FitHeight);
    _pricelabel.frame = CGRectMake(ScreenWidth - 10 * FitWidth - rect2.size.width, _thisWeekHitLabel.frame.origin.y + 5 * FitHeight, rect2.size.width, 15);
    
    _star1ImgeView.frame = CGRectMake(_nameLabel.frame.origin.x, _thisWeekHitLabel.frame.origin.y + _thisWeekHitLabel.frame.size.height + 15 * FitHeight, 10 * FitWidth, 10 * FitHeight);
    
    _star2ImgeView.frame = CGRectMake(_star1ImgeView.frame.origin.x + _star1ImgeView.frame.size.width, _star1ImgeView.frame.origin.y, _star1ImgeView.frame.size.width, _star1ImgeView.frame.size.height);
    
    _star3ImgeView.frame = CGRectMake(_star2ImgeView.frame.origin.x + _star2ImgeView.frame.size.width, _star2ImgeView.frame.origin.y, _star2ImgeView.frame.size.width, _star2ImgeView.frame.size.height);
    
    _star4ImgeView.frame = CGRectMake(_star3ImgeView.frame.origin.x + _star3ImgeView.frame.size.width, _star3ImgeView.frame.origin.y, _star3ImgeView.frame.size.width, _star3ImgeView.frame.size.height);
    
    _star5ImgeView.frame = CGRectMake(_star4ImgeView.frame.origin.x + _star4ImgeView.frame.size.width, _star4ImgeView.frame.origin.y, _star4ImgeView.frame.size.width, _star4ImgeView.frame.size.height);
    
    
    _reviewNumLabel.frame = CGRectMake(_star5ImgeView.frame.origin.x + _star5ImgeView.frame.size.width + 20 * FitWidth, _star5ImgeView.frame.origin.y, rect3.size.width, _star4ImgeView.frame.size.height);
    //CGRectMake(ScreenWidth - 10 * FitWidth - _pricelabel.frame.size.width - rect3.size.width , _thisWeekHitLabel.frame.origin.y, rect3.size.width, 20 * FitHeight);
}

#warning 待完善
-(void)setDataModel:(DataModel *)dataModel
{
    if (_dataModel != dataModel) {
        _dataModel = dataModel;
        _keyWord = nil;
    }
    _nameLabel.text = dataModel.name;
    CGFloat flotHit = [dataModel.thisWeekHit floatValue];
    _thisWeekHitLabel.text = [NSString stringWithFormat:@"周关注量%.1f万次", flotHit / 10000];
    _pricelabel.text = [NSString stringWithFormat:@"%@ %@", dataModel.mark, dataModel.price];
    _reviewNumLabel.text = [NSString stringWithFormat:@"%@点评",dataModel.reviewNum];
    
#pragma mark 拼星星
    NSMutableArray *starArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 5; i++) {
        if (i <= floorf([dataModel.userCommStar floatValue]))
        {
            UIImage *image = [UIImage imageNamed:@"starRed@2x"];
            [starArray addObject:image];
            
        }
        if (i - [dataModel.userCommStar floatValue] < 0.25 && i - [dataModel.userCommStar floatValue] > 0){
            UIImage *image = [UIImage imageNamed:@"starEmpty@2x"];
            [starArray addObject:image];
        }
        if (i - [dataModel.userCommStar floatValue] >= 0.25 && i - [dataModel.userCommStar floatValue] < 0.75) {
            UIImage *image = [UIImage imageNamed:@"starHalf@2x"];
            [starArray addObject:image];
        }
        if (i - [dataModel.userCommStar floatValue] >= 0.75 && i - [dataModel.userCommStar floatValue] < 1) {
            UIImage *image = [UIImage imageNamed:@"starRed@2x"];
            [starArray addObject:image];
        }
        if (i - [dataModel.userCommStar floatValue] >= 1) {
            UIImage *image = [UIImage imageNamed:@"starEmpty@2x"];
            [starArray addObject:image];
        }
    }
    _star1ImgeView.image = starArray[0];
    _star2ImgeView.image = starArray[1];
    _star3ImgeView.image = starArray[2];
    _star4ImgeView.image = starArray[3];
    _star5ImgeView.image = starArray[4];
    
    [_imgsrcView sd_setImageWithURL:[NSURL URLWithString:dataModel.pic]];
    
}

//#pragma mark 搜索的关键字高亮
//-(void)setKeyWord:(NSString *)keyWord
//{
//    if (_keyWord != keyWord) {
//        _keyWord = [keyWord retain];
//    }
//    if (_dataModel.name) {
//        //关键字标红效果
//        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:_dataModel.name];
//        //获取关键字的位置
//        NSRange range = [_dataModel.name rangeOfString:keyWord];
//        
//        //设置关键字属性
//        [attributeString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:15], NSFontAttributeName, nil] range:range];
//        
//        //添加到label
//        _nameLabel.attributedText = attributeString;
//    }
//    
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
